﻿using Sdcb.PaddleOCR;
using SUNRPA.CONTROLS.IMPL.domain;
using SUNRPA.CONTROLS.IMPL.domain.flowItemParam;
using SUNRPA.CONTROLS.IMPL.domain.flowRunningVar;
using SUNRPA.CONTROLS.IMPL.util;
using System.Collections.Concurrent;

namespace SUNRPA.CONTROLS.IMPL.controls {
    /// <summary>
    /// TODO:1暂时不支持位置选取，后续会加上多识别结果位置选取功能
    ///      2添加上搜索范围功能，特定窗口，当前激活窗口
    /// </summary>
    public class ClickTextNAction:BaseControlActions {
        /*public override void Execute(FlowItem item, Dictionary<string, Dictionary<string, RPAVariableDefineWrapper>> globleData, List<FlowItem> flowItems) {
            ClickTextNParam? param = item.ControlParam as ClickTextNParam;
            string searchText = param.SearchText;
            Bitmap currentDeskTop = ImageUtil.TakeScreenShoot();
            OCRResult ocrResults = ImageUtil.PaddleOCR(currentDeskTop);
            List<TextBlock> textBlocks = ocrResults.TextBlocks;
            TextBlock targetBlock = null;
            foreach(TextBlock block in textBlocks) {
                if(searchText.Equals(block.Text)) {
                    targetBlock = block;
                    break;
                }
            }
            if(targetBlock == null) { 
                throw new RpaRuntimeException("没有找到指定文本"); 
            }
            List<OCRPoint> points = targetBlock.BoxPoints;
            int width = Math.Abs(points[0].X - points[1].X);
            int height = Math.Abs(points[0].Y - points[3].Y);
            //获得图片中心坐标
            Point center = ImageUtil.ReCalculatePos(new Point(points[0].X, points[0].Y), new Point(0,0), width, height);
            //将鼠标移动至目标位置
            MouseMoveNAction mva = new MouseMoveNAction();
            mva.moveWithTail(center.X, center.Y);
            if(param.ButtonType == 0) {
                //左键
                switch(param.ClickType) {
                    //单击
                    case 0 : 
                        User32Utils.LeftClick();
                        break;
                    //双击
                    case 1 :
                        User32Utils.LeftDoubleClick();
                        break;
                }
            } else { 
                //右键
                switch(param.ClickType) {
                    //单机
                    case 0 : 
                        User32Utils.RightClick();
                        break;
                    //双击
                    case 1 :
                        User32Utils.RightDoubleClick();
                        break;
                }
            }
        }*/

        public override int Execute(int currentIndex, FlowItem item, ConcurrentDictionary<string, ConcurrentDictionary<string, RPAVariableDefineWrapper>> globleData, List<FlowItem> flowItems) {
            ClickTextNParam? param = item.ControlParam as ClickTextNParam;
            string searchText = param.SearchText;
            this.GetStringVar(ref searchText, globleData);

            int searchArea = param.SearchArea;

            string selectPos = param.SelectPostion;
            this.GetStringVar(ref selectPos, globleData);
            if(!int.TryParse(selectPos, out int selectPostion) || selectPostion <= 0) {
                selectPostion = 1;
            } 


            Bitmap currentDeskTop = ImageUtil.TakeScreenShoot();
            PaddleOcrResult ocrResults = ImageUtil.PaddleOCR(ImageUtil.ConvertToBinary(ImageUtil.BitMap2Mat(currentDeskTop)));

            PaddleOcrResultRegion? targetRegion = null;
            //暂时不支持选取位置
            foreach (PaddleOcrResultRegion region in ocrResults.Regions){
                if(searchText.ToLower().Equals(region.Text.ToLower())) { 
                    targetRegion = region;
                    break;
                }
            }
            if(targetRegion == null) { 
                throw new RpaRuntimeException("没有找到指定文本"); 
            }

            //获得图片中心坐标
            OpenCvSharp.Point center = targetRegion.Value.Rect.Center.ToPoint();
            //将鼠标移动至目标位置
            User32Utils.SetCursorPos(center.X, center.Y);
            if(param.ButtonType == 0) {
                //左键
                switch(param.ClickType) {
                    //单击
                    case 0 : 
                        User32Utils.LeftClick();
                        break;
                    //双击
                    case 1 :
                        User32Utils.LeftDoubleClick();
                        break;
                }
            } else { 
                //右键
                switch(param.ClickType) {
                    //单机
                    case 0 : 
                        User32Utils.RightClick();
                        break;
                    //双击
                    case 1 :
                        User32Utils.RightDoubleClick();
                        break;
                }
            }
            return currentIndex;
        }
    }
}
